package stream;

import java.util.*;
import java.util.stream.Collectors;

public class DistinctDemo {
    public static void main(String[] args) {
        //对数据模2后结果去重
        List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 1, 2, 3, 4);
        List<Integer> integers = demo1(numberList);
        System.out.println(integers);
        List<Integer> result = demo2(numberList);
        System.out.println(result);
    }

    public static List<Integer> demo1(List<Integer> numberList) {
        List<Integer> resultList = new ArrayList<>();
        for (Integer number : numberList) {
            if (number % 2 == 0) {
                resultList.add(number);
            }
        }
        List<Integer> newList = new ArrayList<>();
        for (Integer number : numberList) {
            if (!newList.contains(number)) {
                newList.add(number);
            }
        }
        return newList;
    }

    public static List<Integer> demo2(List<Integer> numberList) {
        List<Integer> newList = new ArrayList();
        Set set = new HashSet();
        for (Integer number : numberList) {
            if (number % 2 == 0) {
                if (set.add(number)) {
                    newList.add(number);
                }
            }
        }
        return newList;
    }

    public static List<Integer> demo3(List<Integer> numberList) {
        List<Integer> result = numberList.stream().filter(n -> n % 2 == 0)
                .distinct().collect(Collectors.toList());
        return result;
    }
}
